package com.renbang.action.crawl;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.action.BaseAction;
import com.renbang.model.common.AjaxResult;
import com.renbang.model.crawl.TaskJob;
import com.renbang.service.crawl.TaskJobService;
import com.renbang.utils.quartz.QuartzSchedule;
/**
 * 该代码由程序生成
 */
@Controller
@RequestMapping(value = "user/crawl/TaskJob")
public class TaskJobAction  extends BaseAction<TaskJob>{
	@Autowired
	TaskJobService taskJobService;
	QuartzSchedule quartzSchedule = new QuartzSchedule();
	
	@RequestMapping(value = "/startJob")
	public @ResponseBody   AjaxResult  start(HttpServletRequest request,TaskJob taskJob) {
		try {
//			 TaskJob taskJob = new TaskJob();
//		     taskJob.setJobName("job1");
//		     taskJob.setGropName("group1");
//		     taskJob.setTriggerName("trigger1");
//		     taskJob.setCron("0/3 * * * * ?");
			 taskJob = taskJobService.findById(taskJob.getId());
		     quartzSchedule.startJob(taskJob);
		     taskJob.setStatus(1);//改变状态
			 taskJobService.modify(taskJob);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK, "startJob");
	}
	
	@RequestMapping(value = "/pauseJob")
	public @ResponseBody AjaxResult pauseJob(HttpServletRequest request,TaskJob taskJob) {
		try {
			taskJob = taskJobService.findById(taskJob.getId());
			quartzSchedule.pauseJob(taskJob);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK,"pauseJob");
	}
	@RequestMapping(value = "/resumeJob")
	public @ResponseBody AjaxResult resumeJob(HttpServletRequest request,TaskJob taskJob) {
		try {
			taskJob = taskJobService.findById(taskJob.getId());
			quartzSchedule.resumeJob(taskJob);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK,"resumeJob");
	}
	@RequestMapping(value = "/stopJob")
	public @ResponseBody AjaxResult removeJob(HttpServletRequest request,TaskJob taskJob) {
		try {
			taskJob = taskJobService.findById(taskJob.getId());
			quartzSchedule.removeJob(taskJob);
			taskJob.setStatus(0);//改变状态
			taskJobService.modify(taskJob);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK,"stopJob");
	}
	@RequestMapping(value = "/shutdownScheduler")
	public @ResponseBody AjaxResult shutdownScheduler(HttpServletRequest request,TaskJob taskJob) {
		try {
			quartzSchedule.shutdownScheduler();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK,"shutdownScheduler");
	}
	
	
	
	
	//打开新增或修改页面
	@RequestMapping(value = "/editForm")
	public String editForm(Integer id, HttpServletRequest request) {
		TaskJob taskJob;
		if (id != null) {
			taskJob = taskJobService.findById(id);
			request.setAttribute("obj", taskJob);
		}
		return "user/crawl/TaskJob-form";
	}
	@RequestMapping(value = "/addOrModify")
	public  @ResponseBody AjaxResult insertOrUpdata(HttpServletRequest request,
	TaskJob taskJob) {
		if(taskJob.getId()!=null){
			int i = taskJobService.modify(taskJob);
			if (i > 0)
				return new AjaxResult(AjaxResult.STATUS_OK, "修改成功");
			else
				return new AjaxResult(AjaxResult.STATUS_OK, "修改失败");
	} else {
		 int i = taskJobService.add(taskJob);
		 if (i > 0)
		 	return new AjaxResult(AjaxResult.STATUS_OK, "增加成功");
		 else
			return new AjaxResult(AjaxResult.STATUS_OK, "增加失败");
	}
		
	}
	
}
